package base.display.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;

public abstract class BasePageDataHolder {

	protected final String TAG = this.getClass().getSimpleName();

	private Bundle pValidBundle;

	private Activity pActivity;

	/**
	 * The activity of the data.
	 * 
	 * @return
	 */
	public Activity getActivity() {
		return pActivity;
	}

	private Fragment pFragment;

	public BasePageDataHolder(Bundle savedInstance, Activity activity, Fragment fragment) {
		initParams(savedInstance, activity, fragment);
	}

	protected void initParams(Bundle savedInstance, Activity activity, Fragment fragment) {
		if (activity == null) {
			activity = fragment.getActivity();
		}
		if (savedInstance == null) {
			if (fragment != null) {
				pValidBundle = fragment.getArguments();
			} else {
				pValidBundle = activity.getIntent().getExtras();
			}
		} else {
			pValidBundle = savedInstance;
		}

		if (pValidBundle == null) {
			pValidBundle = new Bundle();
		}

		pActivity = activity;
		pFragment = fragment;
		parseBundle(getBundle());
	}

	public Bundle getBundle() {
		return pValidBundle;
	}

	public void setBunlle(Bundle bundle, boolean isParseBundle) {
		pValidBundle = bundle;
		if (isParseBundle) {
			parseBundle(pValidBundle);
		}
	}

	/**
	 * Fetch data from the bundle.
	 * 
	 * @param bundleToFetchData
	 *            The bundle to parse.
	 * 
	 */
	public abstract void parseBundle(Bundle bundleToFetchData);

	/**
	 * put data to the target bundle.
	 * 
	 * @param bundleToPutData
	 *            The bundle to put data into .
	 */
	public abstract void packingBundle(Bundle bundleToPutData);

	public Fragment getFragment() {
		return pFragment;
	}

}
